package com.atguigu.blog.service.impl;

import com.atguigu.blog.mapper.CategoryMapper;
import com.atguigu.blog.pojos.Category;
import com.atguigu.blog.result.PageResult;
import com.atguigu.blog.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author CF
 * @date 2023/8/18 21:29
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> queryList() {
        return categoryMapper.findAll();
    }

    @Override
    public PageResult<Category> queryListPage(Integer pageNum, Integer pageSize, Category category) {
        //1.开启分页
        PageHelper.startPage(pageNum, pageSize);
        //2.查询分页数据
        List<Category> categoryList = categoryMapper.searchCategoryByCname(category.getCname());
        //3.创建pageInfo对象，从pageInfo中拿list和list总条数
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        //4.把数据封装到PageResult中
        return new PageResult<>(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public boolean updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    @Override
    public boolean addCategory(Category category) {
        return categoryMapper.addCategory(category);
    }

    @Override
    public boolean deleteCategoryById(Integer id) {
        return categoryMapper.deleteCategoryById(id);
    }
}
